

#include "../../inc/public/public_string/public_string.h"

//************************************************************************

static char root_udisk_path[1024];
static char root_tfcar_path[1024];

static char dev_udisk_path[1024];
static char dev_tfcar_path[1024];


static uint64_t  tfcard_total_size = 0;
static uint64_t  udisk_total_size  = 0;

static uint64_t  tfcard_free_size  = 0;
static uint64_t  udisk_free_size   = 0;


static char tf_total_path[128];
static char tf_free_path[128];


static char ud_total_path[128];
static char ud_free_path[128];

//************************************************************************
static const unsigned char _ToUpperTable[] =
{
    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
    0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
    0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
    0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
    0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
    0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
    0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
    0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
    0x40, 'A',  'B',  'C',  'D',  'E',  'F',  'G',
    'H',  'I',  'J',  'K',  'L',  'M',  'N',  'O',
    'P',  'Q',  'R',  'S',  'T',  'U',  'V',  'W',
    'X',  'Y',  'Z', 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
    0x60, 'A',  'B',  'C',  'D',  'E',  'F',  'G',
    'H',  'I',  'J',  'K',  'L',  'M',  'N',  'O',
    'P',  'Q',  'R',  'S',  'T',  'U',  'V',  'W',
    'X',  'Y',  'Z', 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
    0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
    0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
    0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
    0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
    0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
    0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
    0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
    0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
    0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
    0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
    0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
    0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
    0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
    0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
    0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
    0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
};


static FmtIdx_t ValidFmtTbl_video[] = {
    { ".AVI"	},
    { ".DIVX"	},
    { ".PMP"	},
    { ".3GP"	},
    { ".MP4"	},
    { ".M4V"	},
    { ".F4V"	},
    { ".MPG"	},
    { ".MPEG"	},
    { ".VOB"	},
    { ".RM"		},
    { ".RAM"	},
    { ".RMVB"	},
    { ".FLV" 	},
    { ".WMV" 	},
    { ".ASF" 	},
    { ".MKV"	},
    { ".PSR"	},
    { ".TP" 	},
    { ".TRP"	},
    { ".TS"		},
    { ".M2TS"	},
    { ".MOV"	},   
	{ ".x-matroska"},
    { NULL		},  
};

static FmtIdx_t ValidFmtTbl_photo[] = {
	{ ".JFIF" },
	{ ".JPEG" },
	{ ".PNG"  },
	{ ".BMP"  },
	{ ".JPG"  },
	{ ".GIF"  },
	{ ".HEIC"  },
	{ NULL,   },
};

static FmtIdx_t ValidFmtTbl_music[] = {
	{ ".MP3" },
    { ".MP2" },
    { ".MP1" },
    { ".WMA" },
    { ".OGG" },
    { ".APE" },
    { ".FLAC" },
    { ".AC3" },
    { ".DTS" },
    { ".RA"  },
    { ".AAC" },
    { ".AMR"  },
    { ".ATRC" },
    { ".MID" },
    { ".WAV" },
    { ".AEN" },

	{ NULL,   },
};

	
unsigned int	SLIB_strlen(const char	*pstr)
{
	unsigned int	len;

	len = 0;

	while (( pstr != (char *)0) &&				/* Calc str len until NULL ptr (see Note #2a) ...				*/
		   (*pstr != (char	)0)) {				/* ... or NULL char found	   (see Note #2b).					*/
		len++;
		pstr++;
	}

	return (len);
}

static  char * strchrlast(char * pstr, char srch_char)
{
    char   * pstr_next;
    int64_t  str_len;

    if (pstr == (char *)0)                          /* Rtn NULL if srch str ptr NULL (see Note #2a).                */
    {
        return ((char *)0);
    }
    
    pstr_next  = pstr;
    str_len    = SLIB_strlen(pstr);
    pstr_next += str_len;
    while (( pstr_next != pstr) &&                  /* Srch str from end until beg (see Note #2c) ...               */
           (*pstr_next != srch_char))               /* ... until srch char found   (see Note #2d).                  */
    {
        pstr_next--;
    }

    if (*pstr_next != srch_char)                    /* If srch char NOT found, str points to NULL; ...              */
    {
        return ((char *)0);                         /* ... rtn NULL (see Notes #2b & #2c).                          */
    }

    return (pstr_next);                             /* Else rtn ptr to found srch char (see Note #2d).              */
}

int SLIB_toupper(int c)
{
    return (int)_ToUpperTable[c];
}


int  eLIBs_stricmp(const char * p1_str, const char * p2_str)
{
    char    *p1_str_next;
    char    *p2_str_next;
    int    cmp_val;


    if (p1_str == (char *)0)
    {
        if (p2_str == (char *)0)
        {
            return ((int )0);                      /* If BOTH str ptrs NULL, rtn 0 (see Note #2a).                 */
        }
        cmp_val = (int )0 - (int )SLIB_toupper(*p2_str);
        return (cmp_val);                           /* If p1_str NULL, rtn neg p2_str val (see Note #2b).           */
    }
    if (p2_str == (char *)0)
    {
        cmp_val = (int )SLIB_toupper(*p1_str);
        return (cmp_val);                           /* If p2_str NULL, rtn pos p1_str val (see Note #2c).           */
    }


    p1_str_next = (char *)p1_str;
    p2_str_next = (char *)p2_str;
    p1_str_next++;
    p2_str_next++;
    while ((SLIB_toupper(*p1_str) == SLIB_toupper(*p2_str)) && /* Cmp strs until non-matching char (see Note #2d) ..           */
           (*p1_str      != (char  )0) &&           /* .. or NULL char(s)               (see Note #2e) ..           */
           ( p1_str_next != (char *)0) &&           /* .. or NULL ptr(s) found (see Notes #2f, #2g, & #2h).         */
           ( p2_str_next != (char *)0))
    {
        p1_str_next++;
        p2_str_next++;
        p1_str++;
        p2_str++;
    }


    if (*p1_str != *p2_str)                         /* If strs NOT identical, ...                                   */
    {
         cmp_val = (int )SLIB_toupper(*p1_str) - (int )SLIB_toupper(*p2_str);       /* ... calc & rtn char diff  (see Note #2d1).         */
    }
    else if (*p1_str == (char)0)                    /* If NULL char(s) found, ...                                   */
    {
         cmp_val = 0;                               /* ... strs identical; rtn 0 (see Note #2e).                    */
    }
    else
    {
        if (p1_str_next == (char *)0)
        {
            if (p2_str_next == (char *)0)           /* If BOTH next str ptrs NULL, ...                              */
            {
                cmp_val = (int )0;                 /* ... rtn 0                   (see Note #2f).                  */
            }
            else
            {                                       /* If p1_str_next NULL, ...                                     */
                cmp_val = (int )0 - (int )SLIB_toupper(*p2_str_next);   /* ... rtn neg p2_str_next val (see Note #2g).        */
            }
        }
        else                                        /* If p2_str_next NULL, ...                                     */
        {
            cmp_val = (int )SLIB_toupper(*p1_str_next);        /* ... rtn pos p1_str_next val (see Note #2h).                  */
        }
    }

    return (cmp_val);
}

int  SLIB_strncmp(const char * p1_str, const char * p2_str, int len_max)
{
    char   * p1_str_next;
    char   * p2_str_next;
    int     cmp_val;
    int     cmp_len;


    if (len_max == 0)                               /* If cmp len equals zero, rtn 0                                */
    {
        return ((int )0);
    }

    if (p1_str == (char *)0)
    {
        if (p2_str == (char *)0)
        {
            return ((int )0);                      /* If BOTH str ptrs NULL,  rtn 0      (see Note #2a).           */
        }
        cmp_val = (int )0 - (int )(*p2_str);
        return (cmp_val);                           /* If p1_str NULL, rtn neg p2_str val (see Note #2b).           */
    }
    if (p2_str == (char *)0)
    {
        cmp_val = (int )(*p1_str);
        return (cmp_val);                           /* If p2_str NULL, rtn pos p1_str val (see Note #2c).           */
    }


    p1_str_next = (char *)p1_str;
    p2_str_next = (char *)p2_str;
    p1_str_next++;
    p2_str_next++;
    cmp_len     = 0;
    while ((*p1_str      == *p2_str)   &&           /* Cmp strs until non-matching char (see Note #2d) ..           */
           (*p1_str      != (char  )0) &&           /* .. or NULL char(s)               (see Note #2e) ..           */
           ( p1_str_next != (char *)0) &&           /* .. or NULL ptr(s) found (see Notes #2f, #2g, & #2h);         */
           ( p2_str_next != (char *)0) &&
           ( cmp_len     <  len_max))     /* .. or len nbr chars cmp'd        (see Note #2j).             */
    {
        p1_str_next++;
        p2_str_next++;
        p1_str++;
        p2_str++;
        cmp_len++;
    }


    if (cmp_len == len_max)                         /* If strs     identical for len nbr of chars,                  */
    {
        return ((int )0);                          /* ... rtn 0 (see Note #2j).                                    */
    }

    if (*p1_str != *p2_str)                         /* If strs NOT identical, ...                                   */
    {
         cmp_val = (int )(*p1_str) - (int )(*p2_str);  /* ... calc & rtn char diff  (see Note #2d1).              */
    }
    else if (*p1_str == (char)0)                    /* If NULL char(s) found, ...                                   */
    {
         cmp_val = 0;                               /* ... strs identical; rtn 0 (see Note #2e).                    */
    }
    else
    {
        if (p1_str_next == (char *)0)
        {
            if (p2_str_next == (char *)0)           /* If BOTH next str ptrs NULL, ...                              */
            {
                cmp_val = (int )0;                 /* ... rtn 0                                                    */
            }
            else                                    /* If p1_str_next NULL, ...                                     */
            {
                cmp_val = (int )0 - (int )(*p2_str_next);   /* ... rtn neg p2_str_next val (see Note #2g).        */
            }
        }
        else                                        /* If p2_str_next NULL, ...                                     */
        {
            cmp_val = (int )(*p1_str_next);        /* ... rtn pos p1_str_next val (see Note #2h).                  */
        }
    }

    return (cmp_val);
}


char * SLIB_strstr(char * pstr, char * psrch_str)
{
    unsigned int    str_len;
    unsigned int    srch_str_len;
    unsigned int    srch_len;
    unsigned int    srch_ix;
    int       srch_done;
    int        srch_cmp = 0;
    char      * pstr_srch_ix = 0;

                                                    /* Rtn NULL if str ptr(s) NULL (see Note #2a).                  */
    if (pstr == (char *)0)
    {
        return ((char *)0);
    }
    if (psrch_str == (char *)0)
    {
        return ((char *)0);
    }


    str_len      = SLIB_strlen(pstr);
    srch_str_len = SLIB_strlen(psrch_str);
    if (srch_str_len > str_len)                     /* If srch str len > str len, rtn NULL  (see Note #2b).         */
    {
        return ((char *)0);
    }
    if (srch_str_len == 0)                          /* If srch str len = 0, srch str equal NULL str; ...            */
    {
        pstr_srch_ix = (char *)(pstr + str_len);    /* ... rtn ptr to NULL str found in str (see Note #2c).         */
        return (pstr_srch_ix);
    }

    srch_len  = str_len - srch_str_len;             /* Determine srch len (see Note #2d1).                          */
    srch_ix   = 0;
    srch_done = 0;
    while ((srch_done == 0) && (srch_ix <= srch_len))
    {
        pstr_srch_ix = (char *)(pstr + srch_ix);
        srch_cmp     =  SLIB_strncmp(pstr_srch_ix, psrch_str, srch_str_len);
        srch_done    = (srch_cmp == 0) ? 1 : 0;
        srch_ix++;
    }


    if (srch_cmp != 0)                              /* If srch str NOT found, rtn NULL  (see Note #2d).             */
    {
        return ((char *)0);
    }

    return (pstr_srch_ix);                          /* Rtn ptr to srch str found in str (see Note #2e).             */
}


int __Valid_VideoFileType(const char *file)
{
	char    	*Suffix;
    FmtIdx_t 	*pFmt;
	
    if(NULL == file) 
	{
        return 0;
    }
	
    /* seek to extended name */
    Suffix =  strchrlast((char *)file, '.');
	
    if (NULL == Suffix) 
	{
    	return 0;
    }   
	
    /* look in ebook valid format table */
    pFmt = ValidFmtTbl_video;
	
    while (pFmt->Suffix)
    {    
    	if(eLIBs_stricmp(Suffix, pFmt->Suffix) == 0)
        {
			return 1;        	
        }
        pFmt++;
    }

    return 0;   
}


int __Valid_PhotoFileType(const char *file)
{
	char    	*Suffix;
    FmtIdx_t 	*pFmt;
	
    if(NULL == file) 
	{
        return 0;
    }
	
    /* seek to extended name */
    Suffix =  strchrlast((char *)file, '.');
	
    if (NULL == Suffix) 
	{
    	return 0;
    }   
	
    /* look in ebook valid format table */
    pFmt = ValidFmtTbl_photo;
	
    while (pFmt->Suffix)
    {    
    	if(eLIBs_stricmp(Suffix, pFmt->Suffix) == 0)
        {
			return 1;        	
        }
        pFmt++;
    }

    return 0;   
}


int __Valid_MusicFileType(const char *file)
{
	char    	*Suffix;
    FmtIdx_t 	*pFmt;
	
    if(NULL == file) 
	{
        return 0;
    }
	
    /* seek to extended name */
    Suffix =  strchrlast((char *)file, '.');
	
    if (NULL == Suffix) 
	{
    	return 0;
    }   
	
    /* look in ebook valid format table */
    pFmt = ValidFmtTbl_music;
	
    while (pFmt->Suffix)
    {    
    	if(eLIBs_stricmp(Suffix, pFmt->Suffix) == 0)
        {
			return 1;        	
        }
        pFmt++;
    }

    return 0;   
}


int strcmp_c(const char *s1, const char *s2)
{
	int i;
	char c1, c2;

	for (i = 0, c1 = *s1, c2 = *s2; (c1 != '\0') && (c2 != '\0'); i++)
	{
		c1 = *(s1 + i);
		c2 = *(s2 + i);
		if ((c1 >= 'A') && (c1 <='Z')) c1 += 'a' - 'A';
		if ((c2 >= 'A') && (c2 <='Z')) c2 += 'a' - 'A';
		if (c1 != c2) break;
	}

	return (int)c1 - (int)c2;
}

int strcmp_byex(const char *s1, const char *s2)
{
	if((s1== NULL)||(s2 == NULL))
		return 0;

	char kw1[4]={0x58,0x4e,0x42,'\0'};
	char kw2[4]={0x58,0x4e,0x57,'\0'};
	char kw3[4]={0x42,0x54,0x47,'\0'};

	int i = 0;
	for(i=0;i<3;i++)
	{
		kw1[i]--;
		kw2[i]--;
		kw3[i]--;
	}

	char c1 = *s1;
	
	if ((c1 >= 'A') && (c1 <='Z'))
		c1 += 'a' - 'A';

	if(c1 == 'w')
	{
		if(!strcmp_c(s2, "BYE1")) 
		{
			if(!strcmp_c(s1, kw1)) 
				return 1;
		}
		else if(!strcmp_c(s2, "BYE2")) 
		{
			if(!strcmp_c(s1, kw2)) 
				return 1;
		}
		else
			return 0;
	}
	else if(c1== 'a')
	{
		if(!strcmp_c(s2, "BYE3")) 
		{
			if(!strcmp_c(s1, kw3)) 
				return 1;
		}
		else
			return 0;
	}
	else
	{
		return 0;
	}
	return 0;

}

void root_set_udisk_path(char *path)
{
	memset(root_udisk_path, 0, sizeof(root_udisk_path));
	
	strcpy(root_udisk_path, "/media");
	strcat(root_udisk_path, "/");
	strcat(root_udisk_path, path);
	strcat(root_udisk_path, "/");

	//printf("udisk=%s\n",root_udisk_path);

	return ;
}


void root_get_udisk_path(char *udisk_path,int size)
{
	if(!udisk_path){
		return ;
	}

	if(size < strlen(root_udisk_path)){
		return;
	}

	memcpy(udisk_path,root_udisk_path,strlen(root_udisk_path));

	return ;
}


void root_set_udisk_devpath(char *path)
{
	memset(dev_udisk_path, 0, sizeof(dev_udisk_path));
	
	strcpy(dev_udisk_path, "/dev");
	strcat(dev_udisk_path, "/");
	strcat(dev_udisk_path, path);
	strcat(dev_udisk_path, "/");

	//printf("dev=%s\n",dev_udisk_path);

	return ;
}


void root_get_udisk_devpath(char *udisk_path,int size)
{
	if(!udisk_path){
		return ;
	}

	if(size < strlen(dev_udisk_path)){
		return;
	}

	memcpy(udisk_path,dev_udisk_path,strlen(dev_udisk_path));

	return ;
}



void root_set_tfcard_path(char *path)
{
	memset(root_tfcar_path, 0, sizeof(root_tfcar_path));
	
	strcpy(root_tfcar_path, "/media");
	strcat(root_tfcar_path, "/");
	strcat(root_tfcar_path, path);
	strcat(root_tfcar_path, "/");

	//printf("tfcard=%s\n",root_tfcar_path);

	return ;
}

void root_get_tfcard_path(char *tfcard_path,int size)
{
	if(!tfcard_path){
		return ;
	}

	if(size < strlen(root_tfcar_path)){
		return;
	}

	memcpy(tfcard_path,root_tfcar_path,strlen(root_tfcar_path));

	return ;
}

void root_set_tfcard_devpath(char *path)
{
	memset(dev_tfcar_path, 0, sizeof(dev_tfcar_path));
	
	strcpy(dev_tfcar_path, "/dev");
	strcat(dev_tfcar_path, "/");
	strcat(dev_tfcar_path, path);
	strcat(dev_tfcar_path, "/");

	//printf("devtfcard=%s\n",dev_tfcar_path);

	return ;
}

void root_get_tfcard_devpath(char *tfcard_path,int size)
{
	if(!tfcard_path){
		return ;
	}

	if(size < strlen(dev_tfcar_path)){
		return;
	}

	memcpy(tfcard_path,dev_tfcar_path,strlen(dev_tfcar_path));

	return ;
}



uint64_t root_set_tfcard_size(char *path)
{

	//***************************************************
	//total path 
	
	memset(tf_total_path, 0, sizeof(tf_total_path));
	
	strcpy(tf_total_path, "/dev");
	strcat(tf_total_path, "/");
	strcat(tf_total_path, path);

	//***************************************************
	//free path
	
	
	memset(tf_free_path, 0, sizeof(tf_free_path));
	
	strcpy(tf_free_path, "/media");
	strcat(tf_free_path, "/");
	strcat(tf_free_path, path);

	//***************************************************

	return 0;
}


uint64_t root_get_tfcard_total_size(void)
{


	return tfcard_total_size;
}

uint64_t root_get_tfcard_free_size(void)
{


	return tfcard_free_size;
}


uint64_t root_set_udisk_size(char *path)
{

	//***************************************************
	//total path 
	
	memset(ud_total_path, 0, sizeof(ud_total_path));
	
	strcpy(ud_total_path, "/dev");
	strcat(ud_total_path, "/");
	strcat(ud_total_path, path);

	//***************************************************
	//free path
	
	
	memset(ud_free_path, 0, sizeof(ud_free_path));
	
	strcpy(ud_free_path, "/media");
	strcat(ud_free_path, "/");
	strcat(ud_free_path, path);

	//***************************************************


	return 0;
}



uint64_t root_get_udisk_total_size(void)
{
	struct stat dev;

	if(stat(ud_total_path,&dev) == -1)
	{
		udisk_total_size = 0;
	}
	else
	{
		udisk_total_size = (uint64_t)dev.st_size;
	}

	printf("ud total = %lld,%lldM,%lldG\n",udisk_total_size,udisk_total_size/1024/1024,udisk_total_size/1024/1024/1024);
		

	return udisk_total_size;
}

uint64_t root_get_udisk_free_size(void)
{


	return udisk_free_size;
}





int root_get_storage_path(char *storage_path,int size)
{
	uint8_t udisk_func = 0;
	

	return 0;
}


void substring(char *s, char ch1, char ch2, char *substr)
{
    while (*s && *s++ != ch1);
    while (*s && *s != ch2) *substr++ = *s++;
    *substr = '\0';
}




int32_t is_file_exit(const char *filename)
{
	int	fd 	= -1;

	fd = open(filename,O_RDONLY);
	if(fd < 0){
		//debug_e("file open error %s.\n",filename);
		return 0;
	}else{
		close(fd);
		fd = -1;
		return 1;
	}
}



unsigned char  g_day_per_mon[MONTH_PER_YEAR] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};


unsigned char applib_dt_is_leap_year(unsigned int year)
{
    if ((year % 400) == 0) 
		{
        return 1;
    } 
		else if ((year % 100) == 0)
		{
        return 0;
    } 
		else if ((year % 4) == 0) 
		{
        return 1;
    } 
		else 
		{
        return 0;
    }
}

unsigned char applib_dt_last_day_of_mon(unsigned char month, unsigned short year)
{
    if ((month == 0) || (month > 12)) 
		{
        return g_day_per_mon[1] + applib_dt_is_leap_year(year);
    }

    if (month != 2)
		{
        return g_day_per_mon[month - 1];
    } 
		else 
		{
        return g_day_per_mon[1] + applib_dt_is_leap_year(year);
    }
}

unsigned char applib_dt_dayindex(unsigned short year, unsigned char month, unsigned char day)
{
    char century_code, year_code, month_code, day_code;
    int week = 0;

    century_code = year_code = month_code = day_code = 0;

    if (month == 1 || month == 2)
    {
        century_code = (year - 1) / 100;
        year_code = (year - 1) % 100;
        month_code = month + 12;
        day_code = day;
    }
		else
    {
        century_code = year / 100;
        year_code = year % 100;
        month_code = month;
        day_code = day;
    }

    week = year_code + year_code / 4 + century_code / 4 - 2 * century_code + 26 * ( month_code + 1 ) / 10 + day_code - 1;
    week = week > 0 ? (week % 7) : ((week % 7) + 7);

    return week;
}


void utc_sec_2_mytime(unsigned long utc_sec, mytime_struct *result, char daylightSaving)
{

    long sec = 0;
    long day = 0;
    unsigned short y = 0;
    unsigned char m = 0;
    unsigned short d = 0;
    //unsigned char dst;

	
	utc_sec = utc_sec + 8*60*60;

    if (daylightSaving) 
	{
        utc_sec += SEC_PER_HOUR;
    }
		
    /* hour, min, sec */
    /* hour */
    sec = utc_sec % SEC_PER_DAY;
		
    result->nHour = sec / SEC_PER_HOUR;

		//printf("result->nHour = %d\n",result->nHour);
		
    /* min */
    sec %= SEC_PER_HOUR;
    result->nMin = sec / SEC_PER_MIN;
    //printf("result->nMin = %d\n",result->nMin);
		
    /* sec */
    result->nSec = sec % SEC_PER_MIN;
    //printf("result->nSec = %d\n",result->nSec);
		
    /* year, month, day */
    /* year */
    /* year */
    day = utc_sec / SEC_PER_DAY;
		
    for (y = UTC_BASE_YEAR; day > 0; y++) 
	  {
        d = (DAY_PER_YEAR + applib_dt_is_leap_year(y));
        if (day >= d)
        {
            day -= d;
        }
        else
        {
            break;
        }
    }

    result->nYear = y;

    for (m = 1; m < MONTH_PER_YEAR; m++) 
		{
        d = applib_dt_last_day_of_mon(m, y);
        if (day >= d) 
				{
            day -= d;
        } 
				else
			  {
            break;
        }
    }

		
    result->nMonth = m;
    result->nDay = (unsigned char) (day + 1);

    result->DayIndex = applib_dt_dayindex(result->nYear, result->nMonth, result->nDay);
}


unsigned int mytime_2_utc_sec(mytime_struct *currTime, char daylightSaving)
{

    unsigned int i = 0;
    unsigned int  no_of_days = 0;
    unsigned long utc_time =0;
	  unsigned long temp =0;
    unsigned char dst = 0;

    if (currTime->nYear < UTC_BASE_YEAR) 
	{
        return 0;
    }

    /* year */
    for (i = UTC_BASE_YEAR; i < currTime->nYear; i++) 
	{
        no_of_days += (DAY_PER_YEAR + applib_dt_is_leap_year(i));
    }
    //printf("no_of_days=%d\n",no_of_days);
		
    /* month */
    for (i = 1; i < currTime->nMonth; i++) 
	{
        no_of_days += applib_dt_last_day_of_mon((unsigned char) i, currTime->nYear);
    }
    //printf("no_of_days=%d\n",no_of_days);
    /* day */
    no_of_days += (currTime->nDay - 1);
		
    //printf("no_of_days=%d\n",no_of_days);
		
    /* sec */
    
		//utc_time = (unsigned int) no_of_days * SEC_PER_DAY + 
		//					 (unsigned int) (currTime->nHour * SEC_PER_HOUR +currTime->nMin * SEC_PER_MIN + currTime->nSec);
    //printf("utc_time=%ld\n",utc_time);
		
		utc_time = utc_time  + ((unsigned long)no_of_days * SEC_PER_DAY);
				
		utc_time = (utc_time  + ((unsigned long)currTime->nHour * SEC_PER_HOUR));
		

		utc_time = utc_time  + ((unsigned long)currTime->nMin * SEC_PER_MIN);
		
		utc_time = utc_time  +((unsigned long)currTime->nSec);
		
		
    if (dst && daylightSaving)
	{
        utc_time -= SEC_PER_HOUR;
    }

	utc_time = utc_time - 8*60*60;
	
    
	return utc_time;
}




int generate_system_uuid(char * uuid, int size)
{
	int n;
	char buf[37];
	char *p = buf;
	const char *c = "89ab";

	if(!uuid)
		return -1;

	if(size < 36)
		return -1;
	
	srand((unsigned)platform_get_microsecond());
	
	for(n = 0; n < 4; ++n)
	{
		int b = rand()%255;
		switch( n )
		{
		case 6:
			sprintf(p, "4%x", b%15 );
			break;
		case 8:
			sprintf(p, "%c%x", c[rand()%strlen(c)], b%15 );
			break;
		default:
			sprintf(p, "%02x", b);
			break;
		}

		p += 2;
		switch( n )
		{
		case 3:
		case 5:
		case 7:
		case 9:
			//*p++ = '-';
			break;
		}
	}
	*p = 0;

	memcpy(uuid,buf, strlen(buf));
	
	return 0;
}


int file_is_png(const char *fname)
{
	int len = 0;

	if(!fname){
		return -1;
	}

	len = strlen(fname);
	if(len < 3){
		return -1;
	}

	if (((fname[len - 1] == 'g') && (fname[len - 2] == 'n') && (fname[len - 3] == 'p')) ||
		((fname[len - 1] == 'G') && (fname[len - 2] == 'N') && (fname[len - 3] == 'P')))
	{
		return 0;
	}
	else
	{
		return -1;
	}
}

int file_is_bmp(const char *fname)
{
	int len = 0;

	if(!fname){
		return -1;
	}

	len = strlen(fname);
	if(len < 3){
		return -1;
	}

	if (((fname[len - 1] == 'b') && (fname[len - 2] == 'm') && (fname[len - 3] == 'p')) ||
		((fname[len - 1] == 'B') && (fname[len - 2] == 'M') && (fname[len - 3] == 'P')))
	{
		return 0;
	}
	else
	{
		return -1;
	}
}

int file_is_dat(const char *fname)
{
	int len = 0;

	if(!fname){
		return -1;
	}

	len = strlen(fname);
	if(len < 3){
		return -1;
	}

	if (((fname[len - 1] == 'd') && (fname[len - 2] == 'a') && (fname[len - 3] == 't')) ||
		((fname[len - 1] == 'D') && (fname[len - 2] == 'A') && (fname[len - 3] == 'T')))
	{
		return 0;
	}
	else
	{
		return -1;
	}
}





int file_is_mpeg(const char *fname)
{
	int len = 0;

	if(!fname){
		return -1;
	}

	len = strlen(fname);
	if(len < 3){
		return -1;
	}

	if (((fname[len - 1] == 'm') && (fname[len - 2] == 'p') && (fname[len - 3] == 'e')&& (fname[len - 4] == 'e')) ||
		((fname[len - 1] == 'M') && (fname[len - 2] == 'P') && (fname[len - 3] == 'E')&& (fname[len - 4] == 'G')))
	{
		return 0;
	}
	else
	{
		return -1;
	}
}

int file_is_ts(const char *fname)
{
	int len = 0;

	if(!fname){
		return -1;
	}

	len = strlen(fname);
	if(len < 3){
		return -1;
	}

	if (((fname[len - 1] == 't') && (fname[len - 2] == 's')) ||
		((fname[len - 1] == 'T') && (fname[len - 2] == 'S')))
	{
		return 0;
	}
	else
	{
		return -1;
	}
}


int modify_file_authority(const char *fname)
{
	int ret = -1;
	
	if(!fname){
		return -1;
	}	

#if 0
	ret = chmod(fname, 0777);
	if(ret != 0){
		debug_e("modify error\n");
	}
#endif

	return 0;
}


